/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/3
 */

import './Alert.scss';

import Vue from 'vue';
import Modal from '../Modal.vue';
import html from './Alert.html';

$('body').append(html);

let vm = new Vue({
    el: '#compAlertDialog',
    data: {
        show: false,
        title: '',
        content: '',
        confirmBtnText: '确认',
        callback() {},
        close() {
            vm.show = false;
        }
    },
    components: {
        Modal
    }
});

/**
 *
 * @param config
 * {
 *  title:
 *  content: ,
 *  btnText:
 *  confirmBtnText,
 *  callback
 * }
 * @returns {*}
 */
const alert = function (config) {
    let deferred = $.Deferred();

    let option = $.extend({}, {
        show: true,
        title: config.title || '提示',
        content: config.content,
        confirmBtnText: config.btnText,
        callback() {
            vm.show = false;
            deferred.resolveWith();
        }
    });

    $.map(option, (value, key) => {
        vm[key] = value;
    });

    return deferred;
};

window.originAlert = window.alert;

window.alert = alert;

/**
 * confirm
 * @param config
 * @returns {*}
 */
export default alert;